package com.yc.sole.erp.biz;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.yc.sole.erp.dto.CategoryDto;
import com.yc.sole.erp.repository.CategoryRepository;
import com.yc.sole.erp.request.CategoryQueryReq;
import com.yc.sole.erp.request.CategoryReq;
import com.yc.sole.erp.response.CategoryResp;
import com.yc.sole.framework.utils.TreeUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2023-12-13
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class CategoryBiz {

    private final CategoryRepository categoryRepository;

    /**
     * 新增
     *
     * @param categoryReq
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveCategory(CategoryReq categoryReq) {
        CategoryDto dto = new CategoryDto();
        BeanUtil.copyProperties(categoryReq, dto);
        categoryRepository.saveCategory(dto);
    }

    /**
     * 修改
     *
     * @param categoryReq
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateCategoryById(CategoryReq categoryReq) {
        CategoryDto dto = new CategoryDto();
        BeanUtil.copyProperties(categoryReq, dto);
        categoryRepository.updateCategoryById(dto);
    }

    /**
     * 删除
     *
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteCategoryByIds(Collection<Long> ids) {
        categoryRepository.deleteCategoryByIds(ids);
        // 级联删除下级分类
        for (Long id : ids) {
            deleteCategoryAssociated(id);
        }
    }

    /**
     * 递归删除下级菜单、按钮
     *
     * @param parentId
     */
    private void deleteCategoryAssociated(Long parentId) {
        //根据父id查询下级菜单
        List<CategoryDto> children = categoryRepository.selectByParentId(parentId);
        if (CollUtil.isNotEmpty(children)) {
            Set<Long> ids = children.stream().map(CategoryDto::getId).collect(Collectors.toSet());
            categoryRepository.deleteCategoryByIds(ids);
            for (CategoryDto categoryDto : children) {
                //存在下级菜单，递归修改下级菜单分组、状态、归属子系统
                if (Objects.nonNull(categoryDto.getParentId())) {
                    deleteCategoryAssociated(categoryDto.getId());
                }
            }
        }
    }

    /**
     * 分类树
     *
     * @return
     */
    public List<CategoryResp> selectCategoryTree(CategoryQueryReq categoryQueryReq) {
        List<CategoryDto> categoryDtos = categoryRepository.selectCategoryList(categoryQueryReq);
        return TreeUtil.listToTree(BeanUtil.copyToList(categoryDtos, CategoryResp.class));
    }

    /**
     * 根据id查询分类
     *
     * @param categoryId
     * @return
     */
    public CategoryResp selectCategoryById(Long categoryId) {
        return BeanUtil.copyProperties(categoryRepository.selectCategoryById(categoryId), CategoryResp.class);
    }
}
